package com.oracle.oa.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.MessageBiz;
import com.oracle.oa.biz.MessageToUserBiz;
import com.oracle.oa.biz.ReadCommonMessageBiz;
import com.oracle.oa.entity.Message;
import com.oracle.oa.entity.Messagetouser;
import com.oracle.oa.entity.Readcommonmessage;
import com.oracle.oa.entity.Userinfo;
import com.oracle.oa.util.PageBean;

public class HzxAction extends ActionSupport implements SessionAware {

	private String page;
	private int type;
	private String url;
	private Map<String, Object> session;
	
	MessageBiz mbiz;
	MessageToUserBiz mtubiz;
	ReadCommonMessageBiz rcmbiz;
	
	@Override
	public String execute() throws Exception {
		PageBean p=new PageBean();
		int currentPage=1;
		List pagelist =new ArrayList();
		String select = (String) session.get("up");
		if(("pg").equals(session.get("newpage"))){
			page=null;
		}
		
		if("up".equals(select)){
			List<Message> list=null;
			session.put("messagelist", list);
			url="/admin_cg/hzx/showmessage.jsp";	
		}else if(type==1 || Integer.parseInt(session.get("type").toString())==1){
			//消息分页
			List<Message> plist =new ArrayList();
			List<Message> list = (List) session.get("messagelist");
			p.setAllRow(list.size());
			if(page!=null){
				currentPage=Integer.parseInt(page);
				if(currentPage<1){
					currentPage=1;
				}
				if(currentPage>p.getTotalPage()){
					currentPage=p.getTotalPage();
				}
			}
			if(list!=null && list.size()>0){
				for(int i=0;i<4;i++){
					plist.add(list.get((p.getPageSize()*(currentPage-1))+i));
					if(list.get((p.getPageSize()*(currentPage-1))+i).equals(list.get(list.size()-1))){
						break;
					}
				}
			}
			if(p.getTotalPage()==0){
				p.setTotalPage(1);
			}
			p.setList(plist);
			p.setCurrentPage(currentPage);
			
			List countlist = new ArrayList();
			for(int i = 0;i<plist.size();i++){
				int count = mtubiz.getuserGroupByAnddistinct(plist.get(i).getMessageid());
				List newlist = (List) session.get("userinfoList");
				int c;
				if(count == (newlist.size()-1)){
					c=0;	
				}else{
					c=1;	
				}
				countlist.add(c);
			}
			
			session.put("p", p);
			session.put("messagelist", list);
			session.put("countlist", countlist);
			url="/admin_cg/hzx/showmessage.jsp";	
		}else if(type==2 || Integer.parseInt(session.get("type").toString())==2){
			//收件箱分页
				List list = new ArrayList();
				List<Messagetouser> ulist=mtubiz.getAll(Messagetouser.class);
				Userinfo u = (Userinfo) session.get("userinfo");
				long uid = u.getUserid();
				for(int i=0;i<ulist.size();i++){
					Messagetouser mtu = (Messagetouser) ulist.get(i);
					long mtuuid = mtu.getUserinfo().getUserid();
					if(uid==mtuuid){
						list.add(mtu);
					}
				}
				p.setAllRow(list.size());
				if(page!=null){
					currentPage=Integer.parseInt(page);
					if(currentPage<1){
						currentPage=1;
					}
					if(currentPage>p.getTotalPage()){
						currentPage=p.getTotalPage();
					}
				}
				if(list!=null && list.size()>0){
					for(int i=0;i<4;i++){
						pagelist.add(list.get((p.getPageSize()*(currentPage-1))+i));
						if(list.get((p.getPageSize()*(currentPage-1))+i).equals(list.get(list.size()-1))){
							break;
						}
					}
			}
				if(p.getTotalPage()==0){
					p.setTotalPage(1);
				}
				p.setList(pagelist);
				p.setCurrentPage(currentPage);
				session.put("p", p);
				session.put("messagetouserList", list);
				List mlist=null;
				session.put("messagelist", mlist);
				String newpage="aa";
				session.put("newpage", newpage);
				url="/admin_cg/hzx/inbox.jsp";
		}else if(type==3 || Integer.parseInt(session.get("type").toString())==3){
			//草稿箱分页
			List list = new ArrayList();
			List<Message> messlist=mbiz.getAll(Message.class);
			Userinfo u = (Userinfo) session.get("userinfo");
			if(messlist!=null && messlist.size()>0){
				for(int i=0;i<messlist.size();i++){
					Message m = messlist.get(i);
					if(m.getUserinfo().getUserid()==u.getUserid() && m.getIfpublish()==0){
						list.add(m);
					}
				}
			}
			p.setAllRow(list.size());
			if(page!=null){
				currentPage=Integer.parseInt(page);
				if(currentPage<1){
					currentPage=1;
				}
				if(currentPage>p.getTotalPage()){
					currentPage=p.getTotalPage();
				}
			}
			if(list!=null && list.size()>0){
				for(int i=0;i<4;i++){
					pagelist.add(list.get((p.getPageSize()*(currentPage-1))+i));
					if(list.get((p.getPageSize()*(currentPage-1))+i).equals(list.get(list.size()-1))){
						break;
					}
				}
		}
			if(p.getTotalPage()==0){
				p.setTotalPage(1);
			}
			p.setList(pagelist);
			p.setCurrentPage(currentPage);
			session.put("p", p);
			List<Messagetouser> mtulist=mtubiz.getAll(Messagetouser.class);
			session.put("messagetouserList", mtulist);
			List mlist=null;
			session.put("messagelist", mlist);
			session.put("messageList", list);
			String newpage="aa";
			session.put("newpage", newpage);
			url="/admin_cg/hzx/draftbox.jsp";
		}else if(type==4 || Integer.parseInt(session.get("type").toString())==4){
			List list = new ArrayList();
			List<Message> messlist=mbiz.getAll(Message.class);
			Userinfo u = (Userinfo) session.get("userinfo");
			if(messlist!=null && messlist.size()>0){
				for(int i=0;i<messlist.size();i++){
					Message m = messlist.get(i);
					if(m.getUserinfo().getUserid()==u.getUserid() && m.getIfpublish()==1){
						list.add(m);
					}
				}
			}
			p.setAllRow(list.size());
			if(page!=null){
				currentPage=Integer.parseInt(page);
				if(currentPage<1){
					currentPage=1;
				}
				if(currentPage>p.getTotalPage()){
					currentPage=p.getTotalPage();
				}
			}
			if(list!=null && list.size()>0){
				for(int i=0;i<4;i++){
					pagelist.add(list.get((p.getPageSize()*(currentPage-1))+i));
					if(list.get((p.getPageSize()*(currentPage-1))+i).equals(list.get(list.size()-1))){
						break;
					}
				}
		}
			if(p.getTotalPage()==0){
				p.setTotalPage(1);
			}
			p.setList(pagelist);
			p.setCurrentPage(currentPage);
			session.put("p", p);
			List<Messagetouser> mtulist=mtubiz.getAll(Messagetouser.class);
			session.put("messagetouserList", mtulist);
			List mlist=null;
			session.put("messagelist", mlist);
			session.put("messageList", list);
			String newpage="aa";
			session.put("newpage", newpage);
			url="/admin_cg/hzx/outbox.jsp";
		}else if(type==10){
			url="/admin_cg/hzx/showmessage.jsp";
		}
		
		//查询所有的消息message
			
		//查询用户已读消息表readcommonmessage
		List<Readcommonmessage> readcommonmessagelist=rcmbiz.getAll(Readcommonmessage.class);					
		session.put("readcommonmessagelist", readcommonmessagelist);
		
		
		return SUCCESS;
	}
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}

	public MessageBiz getMbiz() {
		return mbiz;
	}

	public void setMbiz(MessageBiz mbiz) {
		this.mbiz = mbiz;
	}

	public MessageToUserBiz getMtubiz() {
		return mtubiz;
	}

	public void setMtubiz(MessageToUserBiz mtubiz) {
		this.mtubiz = mtubiz;
	}

	public ReadCommonMessageBiz getRcmbiz() {
		return rcmbiz;
	}

	public void setRcmbiz(ReadCommonMessageBiz rcmbiz) {
		this.rcmbiz = rcmbiz;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}


}
